﻿using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;

public class TextureTransfer : MonoBehaviour {

	private List<byte> byteList;
	// Use this for initialization
	void Start () {
		Debug.Log ("Trying to connect...");
		Network.Connect (ClientManager.getInstance().ipAddress, 25003);
		byteList = new List<byte> ();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	[RPC]
	public void TransferTexture(string data, int complete, NetworkMessageInfo info) {
		byte[] bytes = Convert.FromBase64String (data);
		byteList.AddRange(bytes);
		if (complete == 1) {
			Texture2D newTex = new Texture2D (256, 256);
			bool loaded = newTex.LoadImage (byteList.ToArray ());
			if (loaded) {
				Debug.Log ("Texture Data was loaded");
				GetComponent<Renderer> ().material.mainTexture = newTex;
			} else {
				Debug.Log ("Texture Data was not loaded");
			}
			byteList = new List<byte> ();
		} else {
			Debug.Log("Loading more data...");
		}
	}
}
